import axios from 'axios'
import { Notification } from 'element-ui'

import { getRole, getToken } from '@/utils/auth'
// 创建axios实例
const instance = axios.create({
  // baseURL: "http://api.yqty.com/admin/v1/",
  baseURL: process.env.VUE_APP_BASE_API
})

// 请求拦截器
instance.interceptors.request.use(
  (config) => {
    // 配置请求类型
    // config.headers = {
    //   "Conrent-Type": "application/json",
    // };
    // 配置token
    const token = localStorage.getItem('token')
    if (token) {
      // debugger
      console.log(1)
      // config.headers.token = token;

      config.headers.Authorization = `Bearer ` + token
    }
    if (getToken()) {
      // let each request carry token
      // ['X-Token'] is a custom headers key
      // please modify it according to the actual situation
      config.headers['batoken'] = getToken()
    }
    if (getRole()) {
      config.headers['roles'] = getRole()
    }
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

instance.interceptors.response.use(
  (response) => {
    return response.data
  },
  (error) => {
    if (error && error.response) {
      switch (error.response.status) {
        case 400:
          Notification({
            typr: 'error',
            title: '错误',
            message: '错误请求',
            duration: 2
          })
          break
        case 401:
          Notification({
            typr: 'error',
            title: '错误',
            message: '认证失败',
            duration: 2
          })
          break
        case 403:
          Notification({
            typr: 'error',
            title: '错误',
            message: '拒接访问',
            duration: 2
          })
          break
        case 404:
          Notification({
            typr: 'error',
            title: '错误',
            message: '请求错误，未找到资源',
            duration: 2
          })
          break
        case 405:
          Notification({
            typr: 'error',
            title: '错误',
            message: '请求方法未允许',
            duration: 2
          })
          break
        case 408:
          Notification({
            typr: 'error',
            title: '错误',
            message: '请求超时',
            duration: 2
          })
          break
        case 500:
          Notification({
            typr: 'error',
            title: '错误',
            message: '服务端出错',
            duration: 2
          })
          break
        case 501:
          Notification({
            typr: 'error',
            title: '错误',
            message: '网络未实现',
            duration: 2
          })
          break
        case 502:
          Notification({
            typr: 'error',
            title: '错误',
            message: '网络错误',
            duration: 2
          })
          break
        case 503:
          Notification({
            typr: 'error',
            title: '错误',
            message: '服务不可用',
            duration: 2
          })
          break
        case 504:
          Notification({
            typr: 'error',
            title: '错误',
            message: '网络超时',
            duration: 2
          })
          break
        case 505:
          Notification({
            typr: 'error',
            title: '错误',
            message: 'http版本不支持该请求',
            duration: 2
          })
          break
        default:
          Notification({
            typr: 'error',
            title: '错误',
            message: `连接错误${error.response.status}`,
            duration: 2
          })
          break
      }
    } else {
      Notification({
        typr: 'error',
        title: '错误',
        message: '网络错误',
        duration: 2
      })
    }
    return Promise.reject(error.response)
  }
)

export default {
  // get
  get(url, params = {}, headers = {}) {
    return new Promise((resolve, reject) => {
      instance.get(url, { params }, headers).then(
        (res) => {
          resolve(res)
        },
        (err) => {
          reject(err)
        }
      )
    })
  },
  // pos
  post(url, params = {}, headers = {}) {
    return new Promise((resolve, reject) => {
      instance.post(url, params, headers).then(
        (res) => {
          resolve(res)
        },
        (err) => {
          reject(err)
        }
      )
    })
  },
  // put 修改请求
  put(url, params = {}, headers = {}) {
    return new Promise((resolve, reject) => {
      instance.put(url, params, { headers }).then(
        (res) => {
          resolve(res)
        },
        (err) => {
          reject(err)
        }
      )
    })
  },
  // delete 删除请求
  delete(url, params = {}, headers = {}) {
    return new Promise((resolve, reject) => {
      instance.delete(url, { params, headers }).then(
        (res) => {
          resolve(res)
        },
        (err) => {
          reject(err)
        }
      )
    })
  }
}
